﻿/**
  * Ext.ux.LangCombo - Combo pre-configured for languages selection
  * 
  * @author    maple <grantnzm@hotmail.com>
  * @copyright (c) 2008, by maple
  * @date      19. February 2009
  * @version   1.0
  *
  * usage:
  *    {xtype:"langcombo", width:100}
  */

// give id="theme" to your css link in the header in html

Ext.ux.LangCombo = Ext.extend(Ext.form.ComboBox, {
    // configurables
    selectLangText: '选择语言'
    ,lazyRender:false
    ,lazyInit:false
    ,value: '简体中文'

    // {{{
    ,initComponent:function() {

        Ext.apply(this, {
            store: new Ext.data.SimpleStore({
                    fields: ['countryCode', 'countryName'],
                    data: [
                        ['zh_CN', '简体中文']
                        ,['zh_TW', '繁體中文']
                        ,['en', 'English']
                    ]
            })
            ,valueField: 'countryCode'
            ,displayField: 'countryName'
            ,iconClsField: 'countryFlag'
            ,triggerAction: 'all'
            ,mode: 'local'
            ,forceSelection:true
            ,editable:false
            ,fieldLabel: this.selectLangText
        }); // end of apply
    
        this.on('show', this.onXShow(), this);
        this.on('beforeselect', this.onXBeforeSelect, this);
        this.on('select', this.onXSelect, this);
        
        // call parent
        Ext.ux.LangCombo.superclass.initComponent.apply(this, arguments);
    } // end of function initComponent
    // }}}
    
    // private
    ,onXBeforeSelect: function(combo, record, index){
        if (combo.getValue() == record.data.countryCode)
            return false;
    } // end of function onXBeforeSelect
    
    // private
    ,onXSelect:function() {
    	var lang = this.getValue();    	
    	// save in cookie
        if(Ext.state.Manager.getProvider()) {
            Ext.state.Manager.set('lang', lang);
        }
        // set language        
        window.location.search = '?' + Ext.urlEncode({lang:lang}) ;
        
    } // end of function onXSelect
    
    // private
    ,onXShow: function(){
        if(Ext.state.Manager.getProvider()) {
            var lang = Ext.state.Manager.get('lang', this.value);
            this.setValue(lang);
        }
    } // end of function onXShow

}); 

Ext.reg('langcombo', Ext.ux.LangCombo);
// end of extend 
